﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace tactics.MapBuilders
{
    class SnowyFieldBuilder : IMapBuilder
    {
        int width;
        int height;
        float scale;

        TileInfo[] mapInfo;

        #region Properties

        public int Width
        {
            get { return width; }
        }

        public int Height
        {
            get { return height; }
        }

        public float Scale
        {
            get { return scale; }
        }

        public string TileXMLFile
        {
            get { return "Content/TileSets/snowy.xml"; }
        }

        public TileInfo[] MapInfo
        {
            get { return mapInfo; }
        }

        public bool SmoothTerrain
        {
            get { return true; }
        }

        #endregion

        public SnowyFieldBuilder(int widthIn, int heightIn, float scaleIn)
        {
            width = widthIn;
            height = heightIn;
            scale = scaleIn;
        }

        public HeightMap GenerateMap(TileSet tileSet)
        {
            HeightMap heightMap = new HeightMap(width, height, scale, 10f, 30f, 2f, 0);
            heightMap.GeneratePerlinHeightMap();

            GenerateTiles(tileSet, heightMap);
            return heightMap;
        }

        private void GenerateTiles(TileSet tileSet, HeightMap heightMap)
        {
            mapInfo = new TileInfo[width * height];

            HeightMap waterMap = new HeightMap(width, height, scale, 10f, 30f, 2f, 0);
            waterMap.GeneratePerlinHeightMap();

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    int idx = waterMap.AtCoord(x, y);
                    float h = waterMap.GetHeight(x, y);
                    
                    string type = "";
                    if (h > 1.75f)
                    {
                        type = "Water";
                        heightMap.SetHeight(x, y, 0.0f);
                    }
                    else if (h > 0.5f)
                    {
                        type = "Ice";
                        heightMap.SetHeight(x, y, 0.0f);
                    }
                    else
                        type = "Snow";

                    mapInfo[idx] = tileSet.GetTileInfo(type);
                }
            }
        }
    }
}